Imports System.Drawing.Printing

Public Class frmCalcoloTotali


    Private Sub frmCalcoloTotali_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Top = 10
        Me.Left = 10
        txtAnno.Text = Now.Year
        cboMese.SelectedIndex = Month(Now.Date) - 1
        lstCongreghe.DataSource = v_congreghe
    End Sub

    Private Sub cmdChiudi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdChiudi.Click
        Me.Close()
    End Sub

    Private Sub cmdCalcola_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCalcola.Click
        Dim strSql As String = ""
        Dim cnDb As OleDb.OleDbConnection
        Dim cmDb As OleDb.OleDbCommand
        Dim drDb As OleDb.OleDbDataReader

        If completo() Then
            If dtpGiorno.Checked Then
                strSql = String.Format("" & _
                                "SELECT     COUNT(KEY_ID) AS TOTALE_CONTRATTI, " & _
                                "           SUM(NUM_LAMPADE) AS TOTALE_LAMPADE, " & _
                                "           SUM(TOTALE) AS TOTALE_IMPORTO " & _
                                "FROM       CONTRATTI " & _
                                "WHERE      DATA=#{0}# " & _
                                "           AND CONGREGA={1}", dtpGiorno.Value.Month & "/" & dtpGiorno.Value.Day & "/" & dtpGiorno.Value.Year, lstCongreghe.SelectedIndex + 1)
            Else
                strSql = String.Format("" & _
                                "SELECT     COUNT(KEY_ID) AS TOTALE_CONTRATTI, " & _
                                "           SUM(NUM_LAMPADE) AS TOTALE_LAMPADE, " & _
                                "           SUM(TOTALE) AS TOTALE_IMPORTO " & _
                                "FROM       CONTRATTI " & _
                                "WHERE      CANONEDA={0} AND Year([DATA])={1} " & _
                                "           AND CONGREGA={2}", cboMese.SelectedIndex, txtAnno.Text, lstCongreghe.SelectedIndex + 1)
            End If


            cnDb = New OleDb.OleDbConnection(strConnessioneClienti)
            cnDb.Open()
            cmDb = New OleDb.OleDbCommand(strSql, cnDb)
            drDb = cmDb.ExecuteReader

            If drDb.Read Then
                lblTotaleContratti.Text = drDb.Item("TOTALE_CONTRATTI")
                If drDb.Item("TOTALE_CONTRATTI") = 0 Then
                    lblTotaleImporto.Text = 0
                    lblTotaleLampade.Text = 0
                Else
                    lblTotaleImporto.Text = CSng(drDb.Item("TOTALE_IMPORTO"))
                    lblTotaleLampade.Text = CSng(drDb.Item("TOTALE_LAMPADE"))
                End If

            Else
                MsgBox("Nessun contratto per il mese di " & cboMese.Text, MsgBoxStyle.Information, "Ricerca")
            End If
            cnDb.Close()
            cmdStampa.Enabled = True
        End If
    End Sub

    Private Function completo() As Boolean
        If lstCongreghe.SelectedIndex = -1 Then
            MsgBox("Selezionare la congrega", MsgBoxStyle.Critical, "Attenzione")
            Return False
        End If
        Return True
    End Function

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.MdiParent = frmMain
    End Sub

    Private Sub cmdStampa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStampa.Click

        If lblTotaleContratti.Text = "" Then
            MsgBox("Calcolare prima i dati da stampare", MsgBoxStyle.Critical, "Attenzione")
        Else
            Dim Rpt As New CrystalDecisions.CrystalReports.Engine.ReportDocument

            Rpt.Load(pathExe.pathLe & "\Totali.rpt", CrystalDecisions.[Shared].OpenReportMethod.OpenReportByDefault)

            If dtpGiorno.Checked Then
                Rpt.SetParameterValue("lblDataOdierna", "DATA: ")
                Rpt.SetParameterValue("txtDataOdierna", dtpGiorno.Value.ToShortDateString)
                Rpt.SetParameterValue("lblMese", "")
                Rpt.SetParameterValue("txtMese", "")
                Rpt.SetParameterValue("lblAnno", "")
                Rpt.SetParameterValue("txtAnno", "")
            Else
                Rpt.SetParameterValue("lblDataOdierna", "")
                Rpt.SetParameterValue("txtDataOdierna", "")
                Rpt.SetParameterValue("lblMese", "MESE:")
                Rpt.SetParameterValue("txtMese", cboMese.Text)
                Rpt.SetParameterValue("lblAnno", "ANNO:")
                Rpt.SetParameterValue("txtAnno", txtAnno.Text)
            End If
            Rpt.SetParameterValue("txtCongrega", lstCongreghe.SelectedItem.ToString)
            Rpt.SetParameterValue("txtTotaleContratti", CSng(lblTotaleContratti.Text))
            Rpt.SetParameterValue("txtTotaleLampade", CSng(lblTotaleLampade.Text))
            Rpt.SetParameterValue("txtTotaleImporto", CSng(lblTotaleImporto.Text))

            Dim mnu As ToolStripMenuItem

            For Each mnu In frmMain.mnuStampanti.DropDownItems
                If mnu.Checked = True Then
                    Rpt.PrintOptions.PrinterName = mnu.Text
                End If
            Next

            Rpt.PrintToPrinter(1, False, 1, 1)
        End If
        

    End Sub

    Private Sub disattivaStampa(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCongreghe.SelectedIndexChanged, txtAnno.TextChanged, cboMese.SelectedIndexChanged
        cmdStampa.Enabled = False
    End Sub

    Private Sub dtpGiorno_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtpGiorno.MouseLeave
        cboMese.Enabled = Not dtpGiorno.Checked
        txtAnno.Enabled = Not dtpGiorno.Checked
    End Sub

    Private Sub dtpGiorno_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dtpGiorno.MouseUp
        cboMese.Enabled = Not dtpGiorno.Checked
        txtAnno.Enabled = Not dtpGiorno.Checked
    End Sub
End Class